﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BusinessLogic.Data
{
    public class Question
    {
        #region CTOR 
        public Question()
        {
        }

        public Question(int userId, DataRow row, DataTable dtVotes)
        {
            Load(userId, row, dtVotes);
        }
        #endregion CTOR

        public int ID { get; set; }
        
        public int UserID { get; set; }
        
        public string UserName { get; set; }
        
        public string Content { get; set; }
        
        public DateTime CreationDate { get; set; }
        
        public bool Active { get; set; }
        
        public bool AllowVotes { get; set; }
        
        public bool AllowComments { get; set; }
        
        public int Votes { get; set; }
        
        public List<Comment> Comments { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="row"></param>
        /// <param name="dtComments"></param>
        /// <param name="dtVotes"></param>
        public void Load(int userId, DataRow row, DataTable dtVotes)
        {
            ID = row.Field<int>("ID");
            UserID = row.Field<int>("UserID");
            UserName = row.Field<string>("UserName");
            Content = row.Field<string>("Content");
            CreationDate = row.Field<DateTime>("CreationDate");
            Active = row.Field<bool>("Active");

            setQuestionIneractiveStatuses(userId, dtVotes);

            if (dtVotes != null)
            {
                loadVotes(dtVotes);
            }
        }

        #region private

        /// <summary>
        /// set AllowVotes & AllowComments flags
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="dtVotes"></param>
        private void setQuestionIneractiveStatuses(int userId, DataTable dtVotes)
        {
            //if none user or the question is answer there's no need for comments nor votes 
            if (userId > 0 && !Active)
            {
                AllowVotes = AllowComments = false;
            }
            else
            {
                //if user already vote on question vote is blocked
                if (dtVotes == null ||
                    dtVotes.AsEnumerable().Where(row => row.Field<int>("QuestionID").Equals(this.ID) && row.Field<int>("UserID").Equals(userId)).Count() == 0)
                {
                    AllowVotes = true;
                }

                AllowComments = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtVotes"></param>
        private void loadVotes(DataTable dtVotes)
        {
            // load votes count
            //Votes = dtVotes.AsEnumerable().Where(row => row.Field<int>("QuestionID").Equals(this.ID)).Count();

            Votes = (from row in dtVotes.AsEnumerable()
                     where row.Field<int>("QuestionID").Equals(this.ID)
                     select row.Field<int>("Countable Vote")).Sum();
        }

        /// <summary>
        /// load comments list
        /// </summary>
        /// <param name="dtComments"></param>
        private void loadComments(DataTable dtComments)
        {
            Comments = (from rowIter in dtComments.AsEnumerable()
                        where rowIter.Field<int>("QuestionID") == this.ID
                        select new Comment(rowIter)).ToList();
        }

        #endregion private
    }
}
